package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.PigOutService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.vo.PigOutVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 生猪出场信息实现类
 */
@Service("pigOutService")
public class PigOutServiceImpl implements PigOutService{

    @Resource
    private PigOutMapper pigOutMapper;

    @Resource
    private ProInspectInfoMapper proInspectInfoMapper;

    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;

    @Resource
    private CompanyInfoMapper companyInfoMapper;

    @Resource
    private EmployeeInfoMapper employeeInfoMapper;

    /**
     * 检验出场生猪是否检疫
     * @param pid
     * @return
     */
     public Integer checkPig(Integer pid) {
        //生猪信息有效
        PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(pid);
        if(purchaseInfo == null){
            return 1;
        }
        ProInspectInfoExample proInspectInfoExample = new ProInspectInfoExample();
        ProInspectInfoExample.Criteria criteria = proInspectInfoExample.createCriteria();
        criteria.andPurcidEqualTo(pid);
        criteria.andProclineidIsNotNull();
        criteria.andReserve1IsNotNull();
        List<ProInspectInfo> proInspectInfoList = proInspectInfoMapper.selectByExample(proInspectInfoExample);
        if(proInspectInfoList.size() == 0 ){
            //生猪不能出场，没有进行出场检疫
            return 2;
        }else if (proInspectInfoList.get(0).getInspectype() == 1){
            //生猪允许出场
            return 3;
        }else{
            return 2;
        }

    }

    /**
     * 生猪出场信息添加
     * @param pigOut
     */
    public void addPigOut(PigOut pigOut) {
        //查出生猪出场检疫编号
        ProInspectInfoExample proInspectInfoExample = new ProInspectInfoExample();
        ProInspectInfoExample.Criteria criteria = proInspectInfoExample.createCriteria();
        criteria.andPurcidEqualTo(pigOut.getPurGid());
        criteria.andProclineidIsNotNull();
        List<ProInspectInfo> proInspectInfoList = proInspectInfoMapper.selectByExample(proInspectInfoExample);
        Integer pro_id = proInspectInfoList.get(0).getGid();
        pigOut.setProGid(pro_id);
        //设置出场时间
        pigOut.setOuttime(new Date());
        //设置生猪重量
        PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(pigOut.getGid());
        purchaseInfo.setPrcweight(pigOut.getOutweight());
        purchaseInfoMapper.updateByPrimaryKeySelective(purchaseInfo);
        pigOutMapper.insert(pigOut);
    }

    /**
     * 多条件查询生猪出场信息
     * @param beginTime
     * @param endTime
     * @param purGid
     * @return
     */
    public PageInfo findPigout(Date beginTime, Date endTime, Integer purGid,Page page) {
        PigOutExample pigOutExample = new PigOutExample();
        PigOutExample.Criteria criteria = pigOutExample.createCriteria();
        if(purGid != null){
            criteria.andPurGidEqualTo(purGid);
        }
        if(beginTime != null){
            criteria.andOuttimeGreaterThan(beginTime);
        }
        if (endTime != null){
            criteria.andOuttimeLessThan(endTime);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List pigOutList =  pigOutMapper .selectByExample(pigOutExample);
        PageInfo  pageInfo  = new PageInfo(pigOutList);
        List<PigOutVO> pigOutVOList = new ArrayList<PigOutVO>();
        for(PigOut pigOut: (List<PigOut>) pageInfo.getList()){
            CompanyInfo companyInfo1 = companyInfoMapper.selectByPrimaryKey(pigOut.getComGid());
            CompanyInfo companyInfo2 = companyInfoMapper.selectByPrimaryKey(pigOut.getComGid2());
            pigOutVOList.add(new PigOutVO(pigOut,companyInfo1,companyInfo2));
        }
        pageInfo.setList(pigOutVOList);
        return pageInfo;
    }

    /**
     *删除生猪出场信息
     * @param
     */
    @Transactional
    public Integer deletePigOutInfo(String selectRows) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(selectRows);
        for (Integer ID : deleteID){
           pigOutMapper.deleteByPrimaryKey(ID);
        }
        return 1;
    }

    //单个信息删除
    public Integer deletePigOut(Integer id) {
        pigOutMapper.deleteByPrimaryKey(id);
        return 1 ;
    }


    /**
     *根据出场信息ID查询出场信息
     * @param gid
     * @return
     */
    public PigOut findPigOutByID(Integer gid) {
       return pigOutMapper.selectByPrimaryKey(gid);
    }

    /**
     * 更新生猪出场信息
     * @param pigOut
     */
    public void updateInfo(PigOut pigOut) {
        PigOut pigOut1 = pigOutMapper.selectByPrimaryKey(pigOut.getGid());
        pigOut1.setNote(pigOut1.getNote());
        pigOut1.setOuttime(pigOut.getOuttime());
        pigOut1.setOperatorname(pigOut.getOperatorname());
        pigOut1.setPlace(pigOut.getPlace());
        pigOut1.setOutweight(pigOut.getOutweight());
        pigOut1.setRecievertel(pigOut.getRecievertel());
        pigOut1.setWeightunit(pigOut.getWeightunit());
        pigOut1.setReceivername(pigOut.getReceivername());
        pigOutMapper.updateByPrimaryKeySelective(pigOut1);
    }
}
